module.exports = (sequelize, DataTypes) => {
    const Category = sequelize.define(
        'Category',
        {
            id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                autoIncrement: true,
                primaryKey: true
            },
            type: {
                type: DataTypes.STRING(5),
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '分类代码不能为空'
                    }
                }
            },
            name: {
                type: DataTypes.STRING(20),
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '分类名不能为空'
                    }
                },
                comment: '分类名'
            },
            detail: {
                type: DataTypes.STRING,
                comment: '分类详细说明'
            },
            parent_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                references: {
                    model: 'Category',
                    key: 'id'
                },
                comment: '父级ID'
            }
        },
        {
            tableName: 'category'
        }
    )
    Category.associate = function(models) {
        models.Category.hasMany(models.Device, { foreignKey: 'category_id', sourceKey: 'id' })
    }
    return Category
}
